package com.lkd.handler;

import com.google.common.base.Strings;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 工单完成处理
 */
@Slf4j
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompletedMsgHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vmService;
    /**
     * 接收到运维工单完成的消息后，更新售货机状态
     * @param jsonMsg
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {
        TaskCompleteContract contract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        if(contract==null|| Strings.isNullOrEmpty(contract.getInnerCode())){
            log.error("运维工单封装下发时，协议数据有误");
            throw new LogicException("运维工单封装下发时，协议数据有误");
        }

        //1.如果是投放工单，将售货机修改为运营状态
        if(contract.getTaskType()== VMSystem.TASK_TYPE_DEPLOY){
            vmService.updateStatus(contract.getInnerCode(),VMSystem.VM_STATUS_RUNNING);
        }else if(contract.getTaskType()==VMSystem.TASK_TYPE_REVOKE){
            //2.如果是撤机工单，将售货机状态修改为撤机；
            vmService.updateStatus(contract.getInnerCode(),VMSystem.VM_STATUS_REVOKE);
        }
    }
}